package com.zch.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.action.CoreController;
import com.item.dao.model.Role;
import com.item.dao.model.RoleExample;
import com.zch.dao.model.ZhikuType;
import com.zch.dao.model.ZhikuTypeExample;
import com.zch.daoEx.model.ZhikuTypeEx;
import com.zch.service.ZhikuTypeService;

@RequestMapping("zhikuType")
@Controller
@LoginFilter
public class ZhikuTypeController extends CoreController{

	@Autowired
	private ZhikuTypeService zhikuTypeService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(String id)  throws Exception {
		ZhikuTypeExample example = new ZhikuTypeExample();
		example.setOrderByClause("sort_order asc, create_time asc");
		if(StringUtils.isNotBlank(id))
		{
			example.createCriteria().andParentIdEqualTo(id);
		}
		else
		{
			example.createCriteria().andParentIdEqualTo("0");
		}
		
		List<ZhikuType> list = zhikuTypeService.selectByExample(example);
		List<ZhikuTypeEx> listEx = new ArrayList<ZhikuTypeEx>();
		for (ZhikuType zhikuType : list) 
		{
			ZhikuTypeEx zhikuTypeEx = new ZhikuTypeEx();
			BeanUtils.copyProperties(zhikuTypeEx, zhikuType);
			if(StringUtils.isBlank(id) || id.equals("0"))
			{
				zhikuTypeEx.setState("closed");
			}
			listEx.add(zhikuTypeEx);
		}
		
		if (StringUtils.isBlank(id))
		{
			ZhikuTypeEx root = new ZhikuTypeEx();
			root.setParentId("-1");
			root.setId("0");
			root.setLevel(0);
			root.setName("根目录");
			listEx.add(0, root);
		}
		return JSONUtils.serialize(listEx);
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ZhikuType zhikuType){
		if (StringUtils.isBlank(zhikuType.getId())){
			zhikuType.setCreateTime(new Date());
			zhikuType.setModifyTime(new Date());
			zhikuTypeService.insert(zhikuType);
		}else{
			zhikuType.setModifyTime(new Date());
			zhikuTypeService.updateByPrimaryKeySelective(zhikuType);
		}
		return "{\"success\":true}";
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ZhikuType zhikuType = zhikuTypeService.selectByPrimaryKey(id);
		return JSONUtils.serialize(zhikuType);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			zhikuTypeService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findByParentId")
    @ResponseBody
    public String findByParentId(String parentId){
        List<ZhikuTypeEx> list = zhikuTypeService.selectByParentId(parentId);
        return JSONUtils.serialize(list);
    }
	
}